package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaCompany;
import com.sailfish.springbootdemo.pojo.db1.PcbaProcess;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaProcessDao {

    /**
     * 根据工厂id和车间id查询
     *
     * @return
     */
    @Results(id = "pcbaProcessResultMap", value = {
            @Result(column = "pcba_process_id", property = "pcbaProcessId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_process_name_cn", property = "pcbaProcessNameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_process_name_en", property = "pcbaProcessNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "notes", property = "notes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_PROCESS + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and pcba_process_name_cn like CONCAT('%',#{searchValue},'%') or pcba_process_name_en like CONCAT('%',#{searchValue},'%')" +
            "</if>" +
            " order by sort" +
            "</script>")
    List<PcbaProcess> getAllPcbaProcess(@Param("searchValue") String searchValue);

    /**
     * @param pcbaProcessNameCn
     * @return
     */
    @ResultMap(value = "pcbaProcessResultMap")
    @Select("select * from " + DB1TableName.PCBA_PROCESS + " where pcba_process_name_cn = #{pcbaProcessNameCn}")
    List<PcbaProcess> getPcbaProcessByName(@Param("pcbaProcessNameCn") String pcbaProcessNameCn);

    /**
     * 判断有没有重复的
     *
     * @return
     */
    @ResultMap(value = "pcbaProcessResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_PROCESS + " where pcba_process_name_cn = #{pcbaProcessNameCn} " +
            "<if test='pcbaProcessId != null and pcbaProcessId != \"\" '>" +
            " and pcba_process_id != #{pcbaProcessId}" +
            "</if>" +
            "</script>")
    List<PcbaProcess> judgeUniquePcbaProcess(@Param("pcbaProcessId") Integer pcbaProcessId, @Param("pcbaProcessNameCn") String pcbaProcessNameCn);


    /**
     * 根据id删除
     *
     * @param pcbaProcessId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_PROCESS + " where pcba_process_id = #{pcbaProcessId}")
    Integer deletePcbaProcessById(@Param("pcbaProcessId") Integer pcbaProcessId);

    /**
     * 根据车间id删除其下工序
     *
     * @param pcbaWorkshopId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_PROCESS + " where pcba_workshop_id = #{pcbaWorkshopId}")
    Integer deletePcbaProcessByWorkshopId(@Param("pcbaWorkshopId") Integer pcbaWorkshopId);


    /**
     * 根据id查询
     *
     * @param pcbaProcessId
     * @return
     */
    @ResultMap(value = "pcbaProcessResultMap")
    @Select("select * from " + DB1TableName.PCBA_PROCESS + " where pcba_process_id = #{pcbaProcessId}")
    PcbaProcess getPcbaProcessById(@Param("pcbaProcessId") Integer pcbaProcessId);

    /**
     * 新增
     *
     * @param pcbaProcess
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_PROCESS + "(pcba_process_name_cn,pcba_process_name_en,sort,status,notes,create_time) values " +
            "(#{pcbaProcess.pcbaProcessNameCn},#{pcbaProcess.pcbaProcessNameEn},#{pcbaProcess.sort},#{pcbaProcess.status},#{pcbaProcess.notes},#{pcbaProcess.createTime})")
    Integer addPcbaProcess(@Param("pcbaProcess") PcbaProcess pcbaProcess);

    @Update("<script>" +
            "update " + DB1TableName.PCBA_PROCESS +
            "<set>" +
            "<if test='pcbaProcess.pcbaProcessNameCn != null and pcbaProcess.pcbaProcessNameCn != \"\" '>" +
            " pcba_process_name_cn = #{pcbaProcess.pcbaProcessNameCn}," +
            "</if>" +
            "<if test='pcbaProcess.pcbaProcessNameEn != null and pcbaProcess.pcbaProcessNameEn != \"\" '>" +
            " pcba_process_name_en = #{pcbaProcess.pcbaProcessNameEn}," +
            "</if>" +
            "<if test='pcbaProcess.sort != null and pcbaProcess.sort != \"\" '>" +
            " sort = #{pcbaProcess.sort}," +
            "</if>" +
            "<if test='pcbaProcess.status != null and pcbaProcess.status != \"\" '>" +
            " status = #{pcbaProcess.status}," +
            "</if>" +
            "<if test='pcbaProcess.notes != null and pcbaProcess.notes != \"\" '>" +
            " notes = #{pcbaProcess.notes}," +
            "</if>" +
            "<if test='pcbaProcess.updateTime != null '>" +
            " update_time = #{pcbaProcess.updateTime}," +
            "</if>" +
            "</set>" +
            " where pcba_process_id = #{pcbaProcess.pcbaProcessId}" +
            "</script>")
    Integer updatePcbaProcess(@Param("pcbaProcess") PcbaProcess pcbaProcess);
}
